package czh.cocoa.ui.impl;

import czh.cocoa.graphics.CGRect;
import czh.cocoa.ui.*;

public class UISwitch extends UIControl {

	//private final static float THUMB_WIDTH = 54; 
	
	private boolean mOn;
	
	private UIImageView mOnView;
	
	public UISwitch() {
		 
	}
	
	public UISwitch(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		
		UIImage imgoff = UIImage.imageNamed(NSTR("cocoa/switch_off"));  
		UIImageView offview = new UIImageView(imgoff);
		addSubview(offview);
		
		UIImage imgon = UIImage.imageNamed(NSTR("cocoa/switch_on"));  
		UIImageView onview = new UIImageView(imgon);
		addSubview(onview);
		
		CGRect frame = onview.frame();
		setFrame(frame);
		
//		frame.origin.x = frame.size.width - THUMB_WIDTH;
//		offview.setFrame(frame);
//		
//		setClipsToBounds(true);
//      layer().setCornerRadius(THUMB_WIDTH / 2);
		
		mOn = true;
		mOnView = onview;
	}
	
	public void setOn(boolean on) {
		if (mOn != on) {
			mOn = on;
//			CGRect bounds = bounds();
//			bounds.origin.x = on ? 0 : bounds.size.width - THUMB_WIDTH;
//			setBounds(bounds);
			if(on) {
				bringSubviewToFront(mOnView);
			} else {
				sendSubviewToBack(mOnView);
			}
			sendActionsForControlEvents(UIControlEventValueChanged);
		}
	}
	
	public boolean isOn() {
		return mOn;
	}
	
	public void endTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
		super.endTrackingWithTouchWithEvent(touch, event);
		setOn(!mOn);
	}
	
}
